package com.dycx.user.converter;

import java.time.LocalDateTime;

import cn.hutool.core.bean.BeanUtil;
import com.dycx.common.vendor.oss.AliyunOss;
import com.dycx.user.enums.VehicleEnergyEnum;
import lombok.Data;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dycx.user.entity.VehicleDO;
import com.dycx.user.vo.VehicleVO;
import com.dycx.user.dto.VehicleDTO;

/**
 * 车辆表(Vehicle)实体转换类
 *
 * @author wuzongbin
 * @since 2024-12-31 17:36:29
 */


@Data
public class VehicleConverter implements Serializable {
    public static VehicleDO dtoToEntity(VehicleDTO dto) {
        if (dto == null) return null;
        VehicleDO entity = BeanUtil.toBean(dto, VehicleDO.class);
        entity.setDrivingLicenseImage(AliyunOss.delDomain(entity.getDrivingLicenseImage()));
        entity.setVehicleImage(AliyunOss.delDomain(entity.getVehicleImage()));
        return entity;
    }

    public static VehicleVO entityToVo(VehicleDO entity) {
        if (entity == null) return null;
        VehicleVO vo = BeanUtil.toBean(entity, VehicleVO.class);
        vo.setEnergyName(VehicleEnergyEnum.converter(vo.getEnergy()));
        vo.setDrivingLicenseImage(AliyunOss.addDomain(vo.getDrivingLicenseImage()));
        vo.setVehicleImage(AliyunOss.addDomain(vo.getVehicleImage()));
        return vo;
    }

    public static List<VehicleVO> entityToVoList(List<VehicleDO> entityList) {
        List<VehicleVO> voList = new ArrayList<>();
        for (VehicleDO entity : entityList) {
            voList.add(entityToVo(entity));
        }
        return voList;
    }

    public static Page<VehicleVO> entityToVoPage(Page<VehicleDO> page) {

        List<VehicleVO> newRecords = new ArrayList<>();

        List<VehicleDO> records = page.getRecords();
        for (VehicleDO entity : records) {
            newRecords.add(entityToVo(entity));
        }

        Page<VehicleVO> voPage = new Page<>();
        voPage.setRecords(newRecords);
        voPage.setPages(page.getPages());
        voPage.setTotal(page.getTotal());
        voPage.setSize(page.getSize());
        voPage.setCurrent(page.getCurrent());

        return voPage;
    }

}
